package com.step.ems

import android.content.ClipData
import android.content.ClipboardManager
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.res.stringResource
import com.step.ems.component.MyDialog
import com.step.ems.ui.theme.ElevatorMsTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        instance = this
        enableEdgeToEdge()
        setContent {
            ElevatorMsTheme {
                EmsNavHost()
                MyDialog(
                    openDialog = alertDialogEvent.isNotEmpty(),
                    onFirst = { alertDialogEvent = "" },
                    onSecond = { copy(alertDialogEvent) },
                    onDismissRequest = { alertDialogEvent = "" },
                    firstTextId = R.string.dismiss,
                    secondTextId = R.string.copyErrorMsg,
                    title = stringResource(id = R.string.error),
                    content = alertDialogEvent,
                )
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        instance = null
    }

    companion object {
        var instance: MainActivity? = null
            private set
        var alertDialogEvent by mutableStateOf("")
    }

    private fun copy(text: String) {
        val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        val clip: ClipData = ClipData.newPlainText("Text", text)
        clipboard.setPrimaryClip(clip)
        Toast.makeText(this, R.string.copySuccess, Toast.LENGTH_SHORT).show()
    }
}
